﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace Room
{
    public class RCUIUnionNPC : RCUIPlayer
    {
        public RCUIUnionNPC(RoomCharactor owner)
            :base(owner)
        {
            type = UIType.UnionNPC;
        }
        public string npcName;
        public string pageName;
        public string pageOption;
        public int pageLevel;
        public string bubbleText;
        public string id;

        public void Init(string id , string npcName, string pageName, string pageOption, int pageLevel, string bubbleText)
        {
            base.Init(npcName);
            this.npcName = npcName;
            this.pageName = pageName;
            this.pageOption = pageOption;
            this.pageLevel = pageLevel;
            this.bubbleText = bubbleText;
            this.id = id;
        }

        public override void DoStart()
        {
            base.DoStart();

            CreateBoxCollider();
        }
        public override void DoStop()
        {
            base.DoStop();
        }

        protected override void OnClick()
        {
            owner.charactorManager.room.uiFuncs += OpenPage;
        }

        private void OpenPage()
        {
            //var npc = Dict.DictDataManager.Instance.tableNPCDao.GetById(id);

            if (pageOption == "tabIndex=4" && !StaticData.heroOpen)
            {
                NetAPI.SendHttp(OpDefine.CSGetUnionInfo, new CSGetUnionInfo(), (o, ok) =>
                {
                    if (ok)
                    {
                        var info = o.kBody as SCGetUnionInfo;
                        if (info.myRole == 0)
                        {
                            ShowBubble(bubbleText);
                        }
                        else
                        {
                            var str0 =(int) (Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.UnionOpenHeroStatueCoinCost/10000f);
                            var str1 = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.UnionOpenHeroStatueBadgeCost;
                            var str = string.Format(TableTextBlo.GetText("union_hero_str_tip"), str0, str1);
                            var str3 = TableTextBlo.GetText("union_buy_builder");
                            BoxManager.CreateMessageBoxWithCofirmAndCancelButton(str, true, (b) =>
                            {
                                if (b)
                                {
                                    var buy = new CSUnionOpenBuilder();
                                    buy.type = "HeroStatue";
                                    NetAPI.SendHttp(OpDefine.CSUnionOpenBuilder, buy, (o1, ok1) =>
                                    {
                                        if (ok1)
                                        {
                                            StaticData.heroOpen = true;
                                            if(UI.UnionPage.unionInfo!=null)
                                            {
                                                UI.UnionPage.unionInfo.heroStatueOpen = 1;
                                            }
                                            var room = RoomManager.currentRoom as RoomUnion;
                                            room.SetHeroStatue(true);
                                            NetAPI.SendSocket(OpDefine.CSRefreshUnionBuilding, new CSRefreshUnionBuilding());
                                        }
                                    });
                                }
                            }, str3);
                        }
                    }
                });
            }
            else if (pageOption == "tabIndex=5" && !StaticData.treeOpen)
            {
                NetAPI.SendHttp(OpDefine.CSGetUnionInfo, new CSGetUnionInfo(), (o, ok) =>
                {
                    if (ok)
                    {
                        var info = o.kBody as SCGetUnionInfo;
                        if (info.myRole == 0)
                        {
                            ShowBubble(bubbleText);
                        }
                        else
                        {
                            var str0 = (int)(Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.UnionOpenGuardTreeCoinCost/10000f);
                            var str1 = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.UnionOpenGuardTreeBadgeCost;
                            var str = string.Format(TableTextBlo.GetText("union_tree_str_tip"), str0, str1);
                            var str3 = TableTextBlo.GetText("union_buy_builder");
                            BoxManager.CreateMessageBoxWithCofirmAndCancelButton(str, true, (b) =>
                            {
                                if (b)
                                {
                                    var buy = new CSUnionOpenBuilder();
                                    buy.type = "GuardTree";
                                    NetAPI.SendHttp(OpDefine.CSUnionOpenBuilder, buy, (o1, ok1) =>
                                    {
                                        if (ok1)
                                        {
                                            StaticData.treeOpen = true;
                                            if (UI.UnionPage.unionInfo != null)
                                            {
                                                UI.UnionPage.unionInfo.guardTreeOpen = 1;
                                            }
                                            var room = RoomManager.currentRoom as RoomUnion;
                                            room.SetGuardTree(true);

                                            NetAPI.SendSocket(OpDefine.CSRefreshUnionBuilding, new CSRefreshUnionBuilding());
                                        }
                                    });
                                }
                            }, str3);

                        }
                    }
                });
            }
            else
            {
                if (!string.IsNullOrEmpty(pageName))
                {
                    UI.PageManager.Instance.OpenPage(pageName, pageOption);
                }
            }
           
            DataMessageTool.AddMessage(DataMessage.NPC_CLICK, owner);
        }
        

    }
}